package com.java.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.RaceApply;
import com.java.entity.RaceCourse;
import com.java.entity.event.RaceAccountEvent;
import com.java.entity.event.RaceApplyEvent;
import com.java.entity.event.RaceCourseEvent;
import com.java.mapper.RaceAccountMapper;
import com.java.mapper.RaceApplyMapper;
import com.java.mapper.RaceCourseMapper;
import com.java.service.RaceCourseService;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class RaceCourseImpl implements RaceCourseService {
	@Autowired
	private RaceCourseMapper courseMapper;
	@Autowired
	private RaceApplyMapper applyMapper;
	@Autowired
	private RaceAccountMapper accountMapper;

	@Override
	public BaseRespone saveCourse(RaceCourse course) {
		course.setCourseId(MyTools.getUUID()).setCourseTime(new Date());
		int row = courseMapper.insertSelective(course);
		return BaseRespone.result(row, "已保存");
	}

	@Override
	public BaseRespone updateCourse(RaceCourse course) {
		int row = courseMapper.updateSelective(course);
		return BaseRespone.result(row, "已修改");
	}

	@Override
	public BaseRespone delCourse(String courseId) {
		int row = courseMapper.deleteByKey(courseId);
		return BaseRespone.result(row, "已删除");
	}

	@Override
	public TableRespone getRaceCourses(String userId, int page, int size) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (null != userId) {
			params.put("applyUserId", userId);
		}
		List<RaceCourseEvent> list = courseMapper.selectAll(params, page, size);
		for (RaceCourseEvent course : list) {
			course.setCourseUser(accountMapper.selectByKey(course.getCourseUserId()));
			course.setCourseTeacher(accountMapper.selectByKey(course.getCourseTeacherId()));
		}
		long count = courseMapper.selectAllCount(params);
		return new TableRespone(list, count);
	}

	@Override
	public RaceCourseEvent getCourse(String courseId) {
		RaceCourseEvent course = courseMapper.selectByKey(courseId);
		if(null != course) {
			course.setCourseUser(accountMapper.selectByKey(course.getCourseUserId()));
			course.setCourseTeacher(accountMapper.selectByKey(course.getCourseTeacherId()));
		}
		return course;
	}

	@Override
	public BaseRespone saveApply(String userId, String courseId, String file) {
		RaceApply has = applyMapper.selectByInfo(new RaceApply().setApplyUserId(userId)
				.setApplyCourseId(courseId));
		if(null != has) {
			return BaseRespone.error("请不要重复申请");
		}
		RaceApply info = new RaceApply().setApplyId(MyTools.getUUID()).setApplyUserId(userId)
				.setApplyCourseId(courseId).setApplyTime(new Date()).setApplyStatus(0);
		int row = applyMapper.insertSelective(info);
		return BaseRespone.result(row, "已申请");
	}

	@Override
	public BaseRespone saveApplyFile(String applyId, String file) {
		int row = applyMapper.updateSelective(new RaceApply().setApplyId(applyId)
				.setApplyFile(file).setApplyStatus(1));
		return BaseRespone.result(row, "已提交资料，等待审核");
	}

	@Override
	public BaseRespone giveupApply(String applyId) {
		int row = applyMapper.updateSelective(new RaceApply().setApplyId(applyId).setApplyStatus(3));
		return BaseRespone.result(row, "已弃赛");
	}

	@Override
	public BaseRespone passApply(String applyId) {
		int row = applyMapper.updateSelective(new RaceApply().setApplyId(applyId).setApplyStatus(2));
		return BaseRespone.result(row, "已审核");
	}

	@Override
	public BaseRespone applyScore(String applyId, BigDecimal score) {
		int row = applyMapper.updateSelective(new RaceApply().setApplyId(applyId).setApplyScore(score));
		return BaseRespone.result(row, "已打分");
	}

	@Override
	public BaseRespone applyGuide(String applyId, String guideText, String applyGuideFile) {
		int row = applyMapper.updateSelective(new RaceApply().setApplyId(applyId)
				.setApplyGuide(guideText).setApplyGuideFile(applyGuideFile).setApplyGuideTime(new Date()));
		return BaseRespone.result(row, "已指导");
	}

	@Override
	public TableRespone getApplys(String userId, Integer status, int page, int size) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (null != userId) {
			params.put("applyUserId", userId);
		}
		if (null != status) {
			params.put("applyStatus", status);
		}
		List<RaceApplyEvent> list = applyMapper.selectAll(params, page, size);
		for (RaceApplyEvent apply : list) {
			apply.setApplyCourse(courseMapper.selectByKey(apply.getApplyCourseId()));
			apply.setApplyUser(accountMapper.selectByKey(apply.getApplyUserId()));
			if(!MyTools.isEmpty(apply.getApplyCourse())) {
				RaceAccountEvent tea = accountMapper.selectByKey(apply.getApplyCourse().getCourseTeacherId());
				if(!MyTools.isEmpty(tea)) {
					apply.setTeacherName(tea.getAccountName());
				}
			}
		}
		long count = applyMapper.selectAllCount(params);
		return new TableRespone(list, count);
	}

	@Override
	public TableRespone getTeacherApplys(String teacherId, Integer status, int page, int size) {
		List<RaceApplyEvent> list = applyMapper.selectAllByT(teacherId, page, size);
		for (RaceApplyEvent apply : list) {
			apply.setApplyCourse(courseMapper.selectByKey(apply.getApplyCourseId()));
			apply.setApplyUser(accountMapper.selectByKey(apply.getApplyUserId()));
		}
		long count = applyMapper.selectAllCountByT(teacherId);
		return new TableRespone(list, count);
	}

	@Override
	public BaseRespone applyDelete(String applyId) {
		int row = applyMapper.deleteByKey(applyId);
		return BaseRespone.result(row, "已删除");
	}

}
